package com.literature.search.core.handler;

import com.literature.search.core.entity.R.AjaxJson;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: DangKong
 * @Date: 2023/3/1 14:43
 * @Description: 404 处理
 */

@RestController
public class NotFoundHandle implements ErrorController {

    @Override
    public String getErrorPath() {
        return "/error";
    }

    /**
     * 由于除404以外的异常都会被全局异常处理掉，所以走到这里的请求都是404了
     */
    @RequestMapping("/error")
    public Object error(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 记录API访问日志
        AjaxJson aj = AjaxJson.get(404, "not found");
        //SpApilogUtil.endRequest(aj);
        // 返回json消息
        response.setStatus(200);
        return aj;
    }

}